const $=(function(){
    const getParams=(obj)=>{
        var str='';
        for(var key in obj){
            str+=key+"="+obj[key]+"&";
        }
        return str;
    }


    function createXHR(){
        if(window.XMLHttpRequest){
            return new XMLHttpRequest();
        }
        return new ActiveXObject("Msxml2.XMLHTTP");
    }

    const post=(url,data)=>{
        return new Promise(function(resolve,reject){
            let xhr=createXHR();
            xhr.open("post",url);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xhr.responseType="json";
            xhr.send(getParams(data));


            xhr.onreadystatechange=function(){
                if(xhr.status==200&&xhr.readyStates==4){
                    resolve(xhr.response);
                }
            }
        })
    }

    const jsonp=function(settings={}){
        return new Promise(function(resolve,reject){
            let fnName="jQery_"+parseInt(Math.random()*10000);
            window[fnName]=resolve;

            let newUrl=settings.url+"?"+getParams(settings.data)+settings.jsonp+"="+fnName;
            let oScript=document.createElement("script");

            document.body.appendChild(oScript);
            oScript.onload=function(){
                this.remove();
            }
        })
    }
    return {
        post,
        jsonp
    }
})()